<template>
    <div id="create_user_role">
        <el-dialog
        title="添加用户分组、角色"
        :visible.sync="dialogVisible"
        class=''
        top="10vh"
        :close-on-click-modal="false"
        :before-close="handleClose"
        >
            <div class="content_box">
                <el-form v-model= "formData" label-width="80px" label-position="left">
                    <el-form-item label="用户昵称:">
                        <el-input size="small" v-model="userInfo.personName" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="选择分组">
                        <el-cascader
                        style="width: 100%;"
                        size="small"
                        v-model="groupIds"
                        :options="groupsList"
                        :props="{ checkStrictly: true ,value: 'groupId', label: 'groupName',}"
                        placeholder="选择分组"
                        clearable></el-cascader>
                    </el-form-item>
                    <el-form-item label="选则角色:">
                        <el-select :disabled="groupIds.length > 0 ? false : true" size="small" v-model="formData.roleId" placeholder="请选择用户角色" filterable clearable>
                            <el-option
                                v-for="item in rolesList"
                                :key="item.roleId"
                                :label="item.roleName"
                                :value="item.roleId">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="handleClose">取 消</el-button>
                <el-button size="small" type="primary" @click="handleSubmit">提交保存</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
    export default {
        props: ['userInfo', 'isShowRole'],
        computed: {
            dialogVisible: {
                get() {
                    return this.isShowRole;
                },
                set() {}
            }
        },
        data() {
            return {
                formData: {
                    roleId: ''
                },
                rolesList: [],
                groupsList: [],
                groupIds: []
            }
        },
        mounted() {
            this.getRolesList()
            this.getGroupsList()
        },
        methods: {
            /**获取用户信息 */
            getUserDetailById() {
                
            },
            getGroupsList() {
                this.$server.systemControlApi.getSystemGroupsList().then( res => {
                    this.groupsList= res.data
                })
            },
            getRolesList() {
                try{
               
                    this.$server.systemControlApi.getSystemRoleList().then(res => {
                        this.rolesList= res.data
                       
                    })
                }catch(err){this.$paramsError(err)}
            },
            handleClose() {
                this.$confirm('确认关闭？').then(_ => {
                    this.$emit('cancelShow', false)
                }).catch(_ => {});
            },
            
            handleSubmit() {
                try{
                    let params= {
                        userId: this.userInfo.userId,
                        roleId: this.formData.roleId,
                        groupId: this.groupIds.length > 0 ? this.groupIds[this.groupIds.length-1] : ''
                    }
                    this.$server.systemControlApi.addGroupRoleByUser(params).then(res => {
                        this.$emit('cancelShow', false)
                        this.$emit('getUserList')
                    })
                }catch(err) {this.$paramsError(err)}
            },
   
        }
    }
</script>